<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\room;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExtendInterface;
use app\common\model\Building;
use app\common\model\Campus;
use app\common\model\Device;
use app\common\model\room\RoomHospitalDevice;
use app\common\lists\ListsSearchInterface;
use app\common\model\room\RoomHospital;


/**
 * 全院设备关联列表
 * Class RoomHospitalDeviceLists
 * @package app\adminapi\listsroom
 */
class RoomHospitalDeviceLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface {

    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function setSearch(): array {
        return [
//            '=' => ['cid', 'rid', 'status', 'device_id', 'coefficient', 'price', 'base_number', 'reading', 'use_total', 'lift_fee', 'second_pressure_fee', 'total_fee'],
//            'between_time' => ['add_time', 'log_time'],
        ];
    }

    /**
     * @notes 查询条件
     * @return array
     */
    public function queryWhere() {
        $param = $this->request->param();
        $where = [];

        if (!empty($this->adminCid) && $this->adminCid !== 0) {
            $where[] = ['cid', '=', $this->adminCid];
        }

        if (!empty($param['cb_ids'])) {
            if (!empty($param['cb_ids'][0])) {
                $where[] = ['cid', '=', $param['cb_ids'][0]];
            }
            if (!empty($param['cb_ids'][1])) {
                $where[] = ['bid', '=', $param['cb_ids'][1]];
            }
        }

        if (isset($param['room_keyword']) && $param['room_keyword'] != '') {
            $room_ids = RoomHospital::where([
                ['name|address', 'like', '%' . $param['room_keyword'] . '%']
            ])->column("id");
            $where[] = ['rid', 'in', $room_ids];
        }

        if (isset($param['device_keyword']) && $param['device_keyword'] != '') {
            $device_ids = Device::where([
                ['name|device_no', 'like', '%' . $param['device_keyword'] . '%']
            ])->column("id");
            $where[] = ['device_id', 'in', $device_ids];
        }

        if (isset($param['start_time']) && $param['start_time'] != '') {
            $where[] = ['log_time', '>=', $param['start_time']];
        }
        if (isset($param['end_time']) && $param['end_time'] != '') {
            $where[] = ['log_time', '<=', $param['end_time']];
        }

        return $where;
    }

    /**
     * @notes 获取全院设备关联sql
     */
    public function get_query() {
        return RoomHospitalDevice::field(['*'])->with([
            "campus", "building", "room", "device"
        ])->where($this->searchWhere)->where($this->queryWhere());
    }

    /**
     * @notes 获取全院设备关联列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function lists(): array {
        return ($this->get_query())->limit($this->limitOffset, $this->limitLength)->append([
            'cbr_ids', 'cd_ids', 'device_type_text', 'room_user', "sort", "total_fee",
        ])->order([
            'sort' => 'asc', 'rid' => 'asc', 'id' => 'asc',
        ])->select()->toArray();
    }

    /**
     * @notes 获取全院设备关联数量
     * @return int
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function count(): int {
        return ($this->get_query())->count();
    }

    /**
     * @notes 获取扩展
     * @return array
     */
    public function extend() {
        return [];
    }

}
